package com.example.liuke.guangguang.ui;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

import com.example.liuke.guangguang.R;
import com.example.liuke.guangguang.dialog.MProgressDialog;
import com.example.liuke.guangguang.dialog.MStatusDialog;
import com.example.liuke.guangguang.untils.getfilestwo;

import java.util.ArrayList;

public class UsbActivity extends AppCompatActivity {
    private ArrayList<String> imagePathList;
    private String mubiaowenjian="";
    private MProgressDialog mMProgressDialog;
    private int a = 0;
    private Handler mHandler = new Handler();
    private MStatusDialog mMStatusDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_usb);
        registerUsbBroadcast();
    }

    //注册广播的方法
    private void registerUsbBroadcast() {
        IntentFilter iFilter = new IntentFilter();
        iFilter.addAction(Intent.ACTION_MEDIA_EJECT);
        iFilter.addAction(Intent.ACTION_MEDIA_MOUNTED);
        iFilter.addAction(Intent.ACTION_MEDIA_REMOVED);
        iFilter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
        iFilter.addDataScheme("file");
        registerReceiver(mBroadcastReceiver, iFilter);
    }
//    file:///mnt/sda/sda1
    private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(Intent.ACTION_MEDIA_EJECT)) {
                //USB设备移除，更新UI
                Toast.makeText(UsbActivity.this, "拔出", Toast.LENGTH_LONG).show();
            } else if (action.equals(Intent.ACTION_MEDIA_MOUNTED)) {
                //USB设备挂载，更新UI
                String usbPath = intent.getDataString();//（usb在手机上的路径）
                String path = intent.getData().getPath();
                Toast.makeText(UsbActivity.this, "插入", Toast.LENGTH_LONG).show();
                imagePathList = getfilestwo.getImagePathFromSD(UsbActivity.this,"pic");
                Toast.makeText(UsbActivity.this,"    usb在手机上的路径：" + usbPath + "   另外一个：" + path,Toast.LENGTH_SHORT).show();


                /* System.out.println("imagePathListsize " + imagePathList.size() + "    usb在手机上的路径：" + usbPath + "   另外一个：" + path + "  图片路径:" + imagePathList.get(0)
                +"    文件名："+getfilestwo.getFileName(imagePathList.get(0)));*/
                //明天将那个usb路径写成获取不要是写死的

//记得要做保护
                //在做一个dialog，复制资源的时候会把原目录下的所有文件都会删除，您是否需要事先备份，否则全部删除
                /*String sdPath = Environment.getExternalStorageDirectory().toString() + File.separator
                        + "bankvideoandpic"+File.separator;
                String delet = Environment.getExternalStorageDirectory().toString() + File.separator
                        + "bankvideoandpic";
                //先删除
                getfilestwo.deleteAllFile(delet);
                //sd卡中如果不存在新建一个此名的文件夹，不需要加/
                File fileAll = new File(delet);
                if (!fileAll.exists()) {
                    fileAll.mkdirs();
                }
                if(imagePathList!=null&& imagePathList.size()!=0){
                    showProgressDialog01();
                    for(int i=0;i<imagePathList.size();i++){
                        mubiaowenjian=imagePathList.get(i);
                        getfilestwo.copyFile(UsbActivity.this,mubiaowenjian,sdPath+getfilestwo.getFileName(imagePathList.get(i)));
                        a++;
                    }
                    System.out.println("a:///////"+a);
                    if(a==imagePathList.size()){
                        a=0;
                        delayDismissProgressDialog();
                    }
                }*/
            }
        }
    };

    private void showProgressDialog01() {
        mMProgressDialog = new MProgressDialog.Builder(this)
                .isCanceledOnTouchOutside(true)
                .setOnDialogDismissListener(new MProgressDialog.OnDialogDismissListener() {
                    @Override
                    public void dismiss() {
                        mHandler.removeCallbacksAndMessages(null);
                        showStatusDialog01();
                    }
                })
                .build();
        mMProgressDialog.show("复制中...");
    }

    private void delayDismissProgressDialog() {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                mMProgressDialog.dismiss();
            }
        }, 1000);
    }

    private void showStatusDialog01() {
        mMStatusDialog = new MStatusDialog(this);
        mMStatusDialog.show("复制成功", this.getResources().getDrawable(R.drawable.mn_icon_dialog_ok));
    }
}